const BlogType = require("./modules/blogtypeModule");

//添加文章分类
const addBlogType = async (typeInfo) => {
  return await BlogType.create(typeInfo);
};

//删除文章分类
const deleteBlogType = async (id) => {
  //获取受影响文章数量
  const { dataValues } = await BlogType.findByPk(id);
  await BlogType.destroy({ where: { id } });

  return dataValues.articleCount;
};

//修改文章分类
const updateBlogType = async (id, type) => {
  await BlogType.update(type, { where: { id } });

  return await BlogType.findOne({ where: { id } });
};

//获取单个文章分类
const oneBlogType = async (id) => {
  return BlogType.findByPk(id);
};

//获取所有文章分类
const getAllBlogType = async () => {
  return BlogType.findAll();
};

//分类文章数量增加
const addArticleCount = async (id) => {
  const data = await BlogType.findByPk(id);
  data.articleCount++;
  await data.save();
  return;
};

//分类文章数量自减
const reduceArticleCount = async (id) => {
  const data = await BlogType.findByPk(id);
  data.articleCount--;
  await data.save();
  return;
};

module.exports = {
  addBlogType,
  deleteBlogType,
  updateBlogType,
  oneBlogType,
  getAllBlogType,
  addArticleCount,
  reduceArticleCount,
};
